﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace DemoPatternAuthentication.Common
{
    class Common
    {
        public static string GetProjectDirectory()
        {
            return Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
        }

        public static string GetImagesDirectory()
        {
            string imagesPath = GetProjectDirectory() + "\\Resources\\Images";
            return imagesPath;
        }

        public static string GetImageOnStatus()
        {
            return GetImagesDirectory() + "\\on.png";
        }

        public static string GetImageOffStatus()
        {
            return GetImagesDirectory() + "\\off.png";
        }

        public static string GetImageFailedStatus()
        {
            return GetImagesDirectory() + "\\Failed.png";
        }

        public static bool LoginHandler(string pattern)
        {
            if (Encrypt(pattern).Equals(Encrypt("1235789")))
                return true;
            else
                return false;
        }

        /// <summary>
        /// Encrypt string with MD5
        /// </summary>
        /// <param name="toEncrypt">string to encrypt</param>
        /// <returns>string has been encryt</returns>
        public static string Encrypt(string toEncrypt)
        {
            byte[] keyArray;
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes("abc123!"));
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Key = keyArray;
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = tdes.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
    }
}
